package com.cneport.transfer.config;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sun.org.apache.xpath.internal.XPathAPI;
public class HeadSettingConfigureParser{
	private  static Map<String,HeadSettingConfigure> settings = new HashMap<String,HeadSettingConfigure>();	
	static{
		init();
	}
	
	
	public static HeadSettingConfigure getHeadSettingConfigure(String messageType){
		if(!getHeadSettingConfigure().containsKey(messageType))
			throw new HeadSettingConfigureParserException("报文类型["+messageType+"]的配置信息不存在");
		return getHeadSettingConfigure().get(messageType);
	}
	public static Map<String,HeadSettingConfigure> getHeadSettingConfigure(){
		return settings;
	}

	private static void  init() throws HeadSettingConfigureParserException{
		try{
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			docBuilderFactory.setNamespaceAware(true);
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			InputStream is = HeadSettingConfigureParser.class.getResourceAsStream("/Settings.xml");
			// File file = new File("Settings.xml");
			Document config = docBuilder.parse(is);
			NodeList nodelist = XPathAPI.selectNodeList(config, "//DxpMsg/ClNo");
			Node node = null;
			for (int i = 0;i<nodelist.getLength();i++) {
				HeadSettingConfigure headSettingConfigure = new HeadSettingConfigure();
				node = nodelist.item(i);
				String messsagetype = "";
				NamedNodeMap attrs = node.getAttributes();
				if (attrs.getLength() > 0 && attrs != null) {
					Node attr = attrs.getNamedItem("messageType");
					messsagetype = attr.getNodeValue();

				}
				
				String sendid = XPathAPI.selectSingleNode(node, "SendId")
						.getTextContent();
				headSettingConfigure.setSendId(sendid);
				NodeList Receiversnodes = XPathAPI.selectNodeList(node,
						"Receivers/Id");

				List<String> receiverids = new ArrayList<String>();
				for (int index = 0; index < Receiversnodes.getLength(); index++) {
					Node receiveidnode = Receiversnodes.item(index);
					String id = receiveidnode.getTextContent();
					receiverids.add(id);
				}
				headSettingConfigure.setReceiveIds(receiverids);
				
				
				Node FileName = XPathAPI.selectSingleNode(node, "FileName");
				String filename = FileName.getTextContent();
				headSettingConfigure.setFileName(filename);
				
				Node CopMsgId = XPathAPI.selectSingleNode(node, "CopMsgId");
				String copmsgid = CopMsgId.getTextContent();
				headSettingConfigure.setCopMsgId(copmsgid);
				
			
				Date currentdate = new Date();
				XMLGregorianCalendar caldate = com.cneport.transfer.util.DateUtil.convert2XMLGregorianCalendar(currentdate);
				String date = caldate.toString();
				headSettingConfigure.setCreatTime(date);
				
			
				String type = XPathAPI.selectSingleNode(node, "Type")
						.getTextContent();
				headSettingConfigure.setType(type);
				
				
				NodeList Keys = XPathAPI.selectNodeList(node,
						"BizKey/Key");
				List<String> keyList = new ArrayList<String>();
				for (int j = 0; j < Keys.getLength(); j++) {
					Node Key = Keys.item(j);
					String name = Key.getTextContent();
					keyList.add(name);
				}
				headSettingConfigure.setKeyNames(keyList);

				
				String isText = XPathAPI.selectSingleNode(node,
						"//IsText/Encode").getTextContent();
				headSettingConfigure.setIsText(isText);
				
				
				Node RecMapId = XPathAPI.selectSingleNode(node,
						"RecMapId");
				String recmapid = RecMapId.getTextContent();
				headSettingConfigure.setRecMapId(recmapid);
				
				
				String CompressFlag = XPathAPI.selectSingleNode(node,
						"Compress/Flag").getTextContent();
				headSettingConfigure.setCompressFlag(CompressFlag);
				
				
				String CompressType = XPathAPI.selectSingleNode(node,
						"Compress/Type").getTextContent();
				headSettingConfigure.setCompressType(CompressType);
				
				settings.put(messsagetype, headSettingConfigure);
			}

		}catch(Exception ex){
			throw new HeadSettingConfigureParserException("HeadSettingConfigureParser error : "+ex.getMessage(),ex);
		}
	}
	
	
}
